package com.example.maomao.myapplication;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by maomao on 2017/4/22.
 */
public abstract class MyAdapter<T> extends BaseAdapter {
    private ArrayList<T> mData;
    private int mLayoutRes;     //布局id

    public MyAdapter() {
    }

    public MyAdapter(ArrayList<T> mData, int mLayoutRes) {
        this.mData = mData;
        this.mLayoutRes = mLayoutRes;
    }

    //获取泛型集合的长度
    public int getCount(){
        return mData != null ? mData.size() : 0;
    }

    //获取泛型集合指定索引处的项
    public T getItem(int position){
        return mData.get(position);
    }

    //获取泛型集合的索引
    public long getItemId(int position){
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder = ViewHolder.bind(parent.getContext(),convertView,parent,mLayoutRes,
                position);
        bindView(holder,getItem(position));
        return holder.getItemView();
    }

    public abstract void bindView(ViewHolder holder,T object);

    //添加一个元素
    public void add(T data){
        if (mData == null) {
            mData = new ArrayList<>();
        }
        mData.add(data);
        notifyDataSetChanged();
    }

    //望指定位置，添加一个元素
    public void add(int position,T data){
        if (mData == null) {
            mData = new ArrayList<>();
        }
        mData.add(position,data);
        notifyDataSetChanged();
    }

    public void remove(T data){
        if (mData != null) {
            mData.remove(data);
        }
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if (mData != null) {
            mData.remove(position);
        }
        notifyDataSetChanged();
    }

    public void clear() {
        if (mData != null) {
            mData.clear();
        }
        notifyDataSetChanged();
    }

    public static class ViewHolder{
        //存储ListView 的 item 中的View
        private SparseArray<View> mViews;

        //存放 convertView
        private View item;

        //游标
        private int position;

        //Context 上下文
        private Context context;

        //构造方法，完成相关初始化
        private ViewHolder(Context context, ViewGroup parent, int layoutRes){
            mViews = new SparseArray<>();
            this.context = context;
            View convertView = LayoutInflater.from(context).inflate(layoutRes,parent,false);
            convertView.setTag(this);       //设置提示
            item = convertView;             //存放 convertView
        }

        //绑定 ViewHolder与 item
        public static  ViewHolder bind(Context context,View convertView,ViewGroup parent,
                                       int layoutRes, int position){
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder(context, parent, layoutRes);
            } else {
                holder = (ViewHolder) convertView.getTag();     //再次设置提示
                holder.item = convertView;      //再次存放 convertView
            }
            holder.position = position;
            return holder;
        }

        public <T extends View> T getView(int id){
            T t = (T) mViews.get(id);
            if (t == null) {
                t = (T) item.findViewById(id);
                mViews.put(id,t);
            }
            return  t;
        }

        //获取当前条目
        public View getItemView(){
            return item;
        }

        //获取条目位置
        public  int getItemPosition(){
            return position;
        }

        //设置文字
        public ViewHolder setText(int id,CharSequence text){
            View view = getView(id);
            if (view instanceof TextView) {
                ((TextView) view).setText(text);
            }
            return this;
        }

        //设置图片
        public  ViewHolder setImageResource(int id, int drawableRes){
            View view = getView(id);
            if (view instanceof ImageView) {
                ((ImageView) view).setImageResource(drawableRes);
            } else {
                view.setBackgroundResource(drawableRes);
            }
            return  this;
        }

        //设置点击监听
        public ViewHolder setOnClickListener(int id,View.OnClickListener listener){
            getView(id).setOnClickListener(listener);
            return this;
        }

        //设置可见
        public ViewHolder setVisibility(int id,int visible){
            getView(id).setVisibility(visible);
            return this;
        }

        //设置标签(提示)
        public ViewHolder setTag(int id,Object object){
            getView(id).setTag(object);
            return this;
        }

        //其他方法以后可根据需要自行扩展
    }

}


